package cfca.payment.socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import payment.api.system.CMBEnvironment;
import payment.api.system.PaymentUserEnvironment;
import payment.api.system.TxMessenger;
import payment.tools.util.XmlUtil;
//import payment.simulator.institution.tx.common.SendMessage;

public class SocketThread extends Thread {

	Logger logger = Logger.getLogger(SocketThread.class.getName());

	private final int POOL_SIZE=10;//单个CPU线程池大小
	
	
	public void run() {
		try {
			logger.info("running..............");
			PropertiesUtil propertiesUtil = new PropertiesUtil();
			int port1 = Integer.parseInt(propertiesUtil.readValue("port1")
					.trim());
			ServerSocket serversocket = new ServerSocket(port1);
			logger.info("listen on port :" + port1);
			ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
			while (true) {
				Socket socket = serversocket.accept() ; 
				try {
					executorService.execute(new ChildThread(socket));
//					ChildThread childThread = new ChildThread();
//					childThread.setSocket(socket);
//					childThread.run();
				} catch (Exception e) {
					logger.error(e);
					continue;
				}
//				try {
//
//					Socket socket = serversocket.accept();
//					String ip = socket.getInetAddress().getHostAddress();
//					int port = socket.getLocalPort();
//					logger.info("user [ " + ip + ":" + port + " ]");
//
//					InputStream is = socket.getInputStream();
//					OutputStream os = socket.getOutputStream();
//
//					byte[] recData = new byte[10000];
//
//					is.read(recData);// 接收
//					String message = new String(recData);
//
//					logger.info(message);
//
//					// ------------------------------------------------------------------------------------------------
//					Document doc = XmlUtil.createDocument(message.trim());
//					// 报文处理成 明文和密文
//					TxBaseRequest baseRequest = new TxBaseRequest();
//					baseRequest.postProcess(doc);
//					String requestMessage = baseRequest.getRequestMessage(); // 明文
//					String requestSignature = baseRequest.getRequestSignature(); // 密文
//
//					// 消息发送处理
//					TxMessenger txMessenger = new TxMessenger();
//					String txURL = propertiesUtil.readValue("txURL").trim();
//					String[] respMsg = null;
//					respMsg = txMessenger.send(requestMessage,
//							requestSignature, txURL);// 0:message;
//					// 1:signature
//					String plainText = new String(
//							payment.tools.util.Base64.decode(respMsg[0]),
//							"UTF-8");
//
//					logger.info("[message]=[" + respMsg[0] + "]");
//					logger.info("[signature]=[" + respMsg[1] + "]");
//					logger.info("[plainText]=[" + plainText + "]");
//
//					// ------------------------------------------------------------------------------------------------
//
//					String reply = plainText;
//					os.write(reply.getBytes("UTF-8"));
//					os.flush();
//
//					socket.shutdownInput();
//
//					// br.close();
//					os.close();
//					is.close();
//					socket.close();
//				} catch (Exception e) {
//					logger.error(e);
//					continue;
//				}

			}
		} catch (Exception e) {
			logger.error(e);

		}
	}
}
